public with sharing class CommonUtils 
{
	public static Boolean isBlank(String value)
	{
		return value == null || value.length() < 1;
	}
	
	public static Boolean isNotBlank(String value)
	{
		return value != null && value.length() > 0;
	}
	

	/*
	 * isValidEmailFormat : validate the format of the email and that  
	 * 		it is in the expected format.
	 * source : http://java.sun.com/developer/technicalArticles/releases/1.4regex/
	 * returns true if valid, false otherwise
	 */
	public static boolean isValidEmailFormat(String input)
	{
		system.debug('isValidEmailFormat::Input='+input);
		string emailPattern;
      	Pattern p = Pattern.compile('^\\.|^\\@');
      	Matcher m = p.matcher(input);
      	if (m.find())
      		return false;
         
      	//Checks for email addresses that start with www.
      	p = Pattern.compile('^www\\.');
      	m = p.matcher(input);
      	if (m.find()) 
      	{
      		return false;
      	}
      	p = Pattern.compile('[^A-Za-z0-9\\.\\@_\\-~#]+');      
      	m = p.matcher(input);
       
      	if (m.find())
      	{
      		return false;
      	}
      
      	// check whether there is a
      	p = Pattern.compile('.+@.+\\.[a-z]+');
      	m = p.matcher(input);

      	//check whether match is found 
      	if (!m.matches())
      		return false;
      
      	return true;		
	}

   /** 
    * Check if the input parameter is a valid 10 digit phone number that starts with 0.
    * @param phoneNumber
  	* @return boolean value that indicates if phoneNumber is valid.
    */
    public static boolean isValidPhoneNumber(String phoneNumber) 
    {
    	if (isBlank(phoneNumber))
    	{
    		return false;
    	}
    	
        if (Pattern.matches('0[23478][0-9]{8}', phoneNumber))
        { 
            return true;
        }
        else
        { 
            return false;
        }
    }
    
    public static boolean isValidNumber(String value)
    {
    	try
    	{	
       		Double testValue = Double.valueOf(value);
       		return true;
    	}
    	catch(System.Exception e)
    	{
    		return false;
    	}
    }
    
    public static boolean isValidPostcode(String postcode) 
    {
		return Pattern.matches('\\d{3,4}', postcode);
	}
	
	public static boolean isValidBsb(String bsb)
	{
		return Pattern.matches('([0-9]{3}-[0-9]{3})|[0-9]{6}', bsb);
	}
	
	public static boolean isValidAccountNumber(String accountNumber)
	{
		return Pattern.matches('\\d*', accountNumber);
	}
    
    static testMethod void testIsValidNumber()
    {
    	String value = '123123';
    	System.assert(isValidNumber(value));
    	value = '123gh56';
    	System.assert(!isValidNumber(value));
    	
    }
    
    static testMethod void testIsValidAccountNumber()
    {
    	String value = '123123';
    	System.assert(isValidAccountNumber(value));
    	value = '123gh56';
    	System.assert(!isValidAccountNumber(value));
    	
    }
    
    static testMethod void testIsValidBsb()
    {
    	String bsb = '112-879';
    	System.assert(CommonUtils.isValidBsb(bsb));
    	bsb = '112879';
    	System.assert(CommonUtils.isValidBsb(bsb));
    	bsb = '11s879';
    	System.assert(!CommonUtils.isValidBsb(bsb));
    	bsb = '2879';
    	System.assert(!CommonUtils.isValidBsb(bsb));
    	bsb = '1128798';
    	System.assert(!CommonUtils.isValidBsb(bsb));
    	
    }
    
    static testMethod void testThis()
	{
		String value = null;
		System.assert(CommonUtils.isBlank(value));
		System.assert(CommonUtils.isNotBlank(value) == false);
		value = 'something';
		System.assert(CommonUtils.isBlank(value) == false);
		System.assert(CommonUtils.isNotBlank(value));
		//Now validateEmail test
        String input = 'joe@hotmail.com';
        boolean expected = true; 
        
        boolean actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);
        
        input = 'www.john@microsoft.com';
        expected = false;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);
        
        input = 'john@microsoftcom';
        expected = false;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);
        
        input = '@microsoftcom';
        expected = false;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);

        input = '.john@microsoft.com';
        expected = false;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);
        
        input = 'john.microsoft.com';
        expected = false;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);

        input = 'l3tt3rsAndNumb3rs@domain.com';
        expected = true;
        actual = CommonUtils.isValidEmailFormat(input);
        System.assert(actual == expected);
		
		
		//Validate phone number test
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);

		input = '';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	

		input = '0987654321';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);
		
		input = '1987654321';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	

		input = '0234567891';
		expected = true;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	

		input = '0wertttaa';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	

		input = '0512345678';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	
		
		input = '0234567890';
		expected = true;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	
		
		input = '0612345678';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	

		input = '024567890';
		expected = false;
		actual = CommonUtils.isValidPhoneNumber(input);
		System.assert(actual == expected);	
	}
}